<?php

// ##############################################################################||
// #                                                                 
// #   MySmartBB Version 1.7.0	                                      
// #   http://www.MySmartBB.com                                      
// #   Copyright (c) 2008 by MySmartBB team                           
// #   license http://opensource.org/licenses/gpl-license.php GNU Public License
// #                                                             
// #   filename : search.php                                        
// #   search system             
// #                                                                  
// ##############################################################################||


// ** General definitions **

     define('NOT_IN_INDEX',0);
     include('common.php');
	 include('includes/pager.php');

     if ($groupper_row['search_allow'] == 0)
     {
	     $SF->html_title_page('محرك البحث - (Powered By MySmartBB Universal)');
         $SF->error('المعذرة، لا يمكنك استخدام محرك البحث');
     }

     if ($_GET['search'] == 1)
     {   

         if ($_GET['index'] == 1)
         {
			$SF->html_title_page('محرك البحث - (Powered By MySmartBB Universal)');
             $section_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "section WHERE section_password <> '1' AND sec_section <> '1' ORDER BY sort ASC");

             $sections_row = array();
             while ($section_row = $DB->sql_fetch_array($section_query))
             {
                 $sections_row[] = $section_row;
                 $Smarty->assign('sections_row',$sections_row);
             }

             $Smarty->assign('path_bar','محرك البحث');
         	 $Smarty->display('path-bar.tpl');
             $Smarty->display('search-home.tpl');

             $SF->FastMove();

             $Smarty->display('footer.tpl');
         }

         if ($_GET['start'] == 1)
         {
		 
		 	$page  = intval($_GET['page']);
			$page = ($page == 0 ? 1 : $page);
			$start = ($info_row['perpage'] * ($page-1));

			$RP = new Pager($page);
			
             $SF->html_title_page('نتائج البحث - (Powered By MySmartBB Universal)');
             $T1 = $_GET['T1'];
             $T2 = $_GET['T2'];
             if (empty($T1) and empty($T2))
             {
                 $SF->error('يرجى تعبئة كافة المعلومات');
             }
             if ($_GET['D1'] == 'all')
             {
                 if (empty($T2) and !empty($T1))
                 {
                     $search_sql = "SELECT * FROM " . $db_prefix . "subject WHERE sec_subject<>'1' AND delete_topic<>'1' AND (text LIKE '%" . $T1 . "%' OR title LIKE '%" . $T1 . "%') ORDER BY native_write_time DESC";
                 }

                 if (!empty($T2) and !empty($T1))
                 {
                     $search_sql = "SELECT * FROM " . $db_prefix . "subject WHERE writer='" . $T2 . "' AND delete_topic<>'1' AND sec_subject<>'1' AND (text LIKE '%" . $T1 . "%' OR title LIKE '%" . $T1 . "%') ORDER BY native_write_time DESC";
                 }

                 if (!empty($T2) and empty($T1))
                 {
                     $search_sql = "SELECT * FROM " . $db_prefix . "subject WHERE writer='" . $T2 . "' AND delete_topic<>'1' AND sec_subject<>'1' ORDER BY native_write_time DESC";
                 }
             }
             else
             {
                 $s_id = intval($_GET['D1']);

                 if (empty($T2) and !empty($T1))
                 {
                     $search_sql = "SELECT * FROM " . $db_prefix . "subject WHERE section='" . $s_id . "' AND delete_topic<>'1' AND sec_subject<>'1' AND (text LIKE '%" . $T1 . "%' OR title LIKE '%" . $T1 . "%') ORDER BY native_write_time DESC";
                 }

                 if (!empty($T2) and !empty($T1))
                 {
                     $search_sql = "SELECT * FROM " . $db_prefix . "subject WHERE section='" . $s_id . "' AND writer='" . $T2 . "' AND delete_topic<>'1' AND sec_subject<>'1' AND (text LIKE '%" . $T1 . "%' OR title LIKE '%" . $T1 . "%') ORDER BY native_write_time DESC";
                 }

                 if (!empty($T2) and empty($T1))
                 {
                     $search_sql = "SELECT * FROM " . $db_prefix . "subject WHERE writer='" . $T2 . "' AND delete_topic<>'1' AND section='" . $s_id . "' AND sec_subject<>'1' ORDER BY native_write_time DESC";
                 }
                 
             }
			 
			  $search_query = $DB->sql_query($search_sql . " LIMIT $start," . $info_row['perpage']);

         $search_num = $DB->sql_num_rows($search_query);
         if ($search_num == 0)
         {
			$SF->error('عُذراً ولكن لايوجد نتائج , حاول البحث بصيغة مختلفة');
         }
         
             $searchs_row = array();
             while ($search_row = $DB->sql_fetch_array($search_query))
             {
                 $searchs_row[] = $search_row;
                 $sects_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "section WHERE id = '".$search_row['section']."'");

                 if (!isset($sects_row))
                 {
                     $sects_row = array();
                 }

                 while ($secs_row = $DB->sql_fetch_array($sects_query))
                 {
                     $sects_row[] = $secs_row;
                     $Smarty->assign('sects_row',$sects_row);
                 }

                 $Smarty->assign('searchs_row',$searchs_row);
             }
			 
 			 $RP->SetPagerN($info_row['perpage'],$DB->sql_num_rows($DB->sql_query($search_sql)));

			 $Smarty->assign('print_page',$RP->PageNum(preg_replace('/\&page\=([0-9]+)/si', '', $_SERVER['QUERY_STRING'])));

             $Smarty->assign('T1',$T1);

             $Smarty->assign('path_bar','<a href="search.php?search=1&index=1">محرك البحث</a> -> نتائج البحث');
             $Smarty->display('search-results.tpl');

             $SF->FastMove();

             $Smarty->display('footer.tpl');
         }
     }

?>